package com.example.yoho.mvp.view.fragment;




import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.blankj.utilcode.util.EncryptUtils;
import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerRegisterComponent;
import com.example.yoho.di.moudles.RegisterMoudles;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.RegisterContract;
import com.example.yoho.mvp.model.entity.RegisterEntity;
import com.example.yoho.mvp.presenter.RegisterPresenter;
import com.example.yoho.mvp.view.activity.UserActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegisterFragment extends BaseFragment<RegisterPresenter> implements RegisterContract.View, View.OnClickListener {


    private android.widget.ImageView imReturn;
    private android.widget.ImageView tvFinish;
    private android.widget.EditText registerUser;
    private android.widget.EditText registerPwd;
    private android.widget.Button btRegister;


    @Override
    public int bondLayout() {
        return R.layout.fragment_register;
    }

    @Override
    public void Inject() {
        DaggerRegisterComponent.builder().registerMoudles(new RegisterMoudles(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        imReturn = (ImageView) findviewByIds(R.id.im_return);
        imReturn.setOnClickListener(this);
        tvFinish = (ImageView) findviewByIds(R.id.tv_finish);
        tvFinish.setOnClickListener(this);
        registerUser = (EditText) findviewByIds(R.id.register_user);
        registerPwd = (EditText) findviewByIds(R.id.register_pwd);
        btRegister = (Button) findviewByIds(R.id.bt_register);
        btRegister.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void showRegister(RegisterEntity registerEntity) {
        Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_finish:
                LoginFragment loginFragment=new LoginFragment();
                getActivity().finish();

                break;
            case R.id.im_return:
                UserActivity activity = (UserActivity) getActivity();
                activity.ViewPages(0);
                break;
            case R.id.bt_register:
                String user = registerUser.getText().toString();
                String pwd = registerPwd.getText().toString();

                byte[] buf = EncryptUtils.encryptRSA(pwd.getBytes(),
                        android.util.Base64.decode(Domain_Url.privateKey.getBytes(), android.util.Base64.DEFAULT)
                        ,false,"RSA/ECB/PKCS1Padding");
                byte[]buff = android.util.Base64.encode(buf, android.util.Base64.DEFAULT);

                String rasPwd = new String(buff);
                Log.e("yskpwd", "onClick: "+rasPwd );
                mPresenter.RegisterAll(user,rasPwd);
                break;
        }
    }
}
